package cn.ad.server.persistence.dao;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import cn.ad.server.persistence.typehandler.ResourceTypeEnumStatusTypeHandler;
import cn.ad.server.domain.ad.StrategyGroup;
import cn.ad.server.domain.ad.StrategyGroupPlatformMap;
import org.springframework.stereotype.Component;

@Component
@Mapper
public interface StrategyGroupDao {

	/**
	 * 策略分组
	 * @param id
	 * @return
	 */
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "name", property = "name"),
			@Result(column = "campaign_id", property = "campaignId"),
			@Result(column = "resource_type", property = "resourceType",typeHandler=ResourceTypeEnumStatusTypeHandler.class),
			@Result(column = "day_budget", property = "dayBudget"),
			@Result(column = "speed_mode", property = "speedMode"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "status", property = "status"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "id",property ="strategyGroupPlatformMaps",many = @Many(select = "selectSGPlatformMapByGid" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from strategy_group where id = #{id}")
	StrategyGroup selectById(@Param("id") long id);
	
	/**
	 * 策略分组对应的map。
	 * @param id
	 * @return
	 */
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "strategy_group_id", property = "strategyGroupId"),
			@Result(column = "platform_id", property = "platformId"),
			@Result(column = "status", property = "status"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from strategy_group_platform_map where strategy_group_id = #{strategyGroupId} and status = 1")
	List<StrategyGroupPlatformMap> selectSGPlatformMapByGid(@Param("strategyGroupId") long id);

}
